using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    public class DbConnUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static string GetHost(string connStr)
        {
            string host = "localhost";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("Data Source\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match match = regex.Match(connStr);
            if (match.Success)
            {
                host = match.Groups[1].Value;
            }
            else
            {
                regex = new System.Text.RegularExpressions.Regex("Server\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                match = regex.Match(connStr);
                if (match.Success)
                {
                    host = match.Groups[1].Value;
                }
            }
            if (host.IndexOf("\\") > 0)
            {
                host = host.Substring(0, host.IndexOf("\\"));
            }
            return host;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static string GetDbServerInstance(string connStr)
        {
            string host = "localhost";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("Data Source\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match match = regex.Match(connStr);
            if (match.Success)
            {
                host = match.Groups[1].Value;
            }
            else
            {
                regex = new System.Text.RegularExpressions.Regex("Server\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                match = regex.Match(connStr);
                if (match.Success)
                {
                    host = match.Groups[1].Value;
                }
            }
            string instance = string.Empty;
            if (host.IndexOf("\\") > 0)
            {
                instance = host.Substring(host.IndexOf("\\") + 1);
            }
            return instance;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static string GetDatabaseName(string connStr)
        {
            string dbName = "master";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("Initial\\s+Catalog\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match match = regex.Match(connStr);
            if (match.Success)
            {
                dbName = match.Groups[1].Value;
            }
            else
            {
                regex = new System.Text.RegularExpressions.Regex("Database\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                match = regex.Match(connStr);
                if (match.Success)
                {
                    dbName = match.Groups[1].Value;
                }
            }
            return dbName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static AuthenticationType GetAuthenticationType(string connStr)
        {
            AuthenticationType authType = AuthenticationType.WindowsAuthentication;
            System.Text.RegularExpressions.Regex authRegex = new System.Text.RegularExpressions.Regex("Integrated\\s+Security\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match authMatch = authRegex.Match(connStr);
            if (authMatch.Success)
            {
                try
                {
                    bool isWinAuth = bool.Parse(authMatch.Groups[1].Value);
                    if (!isWinAuth)
                    {
                        string uid = DbConnUtil.GetUserID(connStr);
                        string pwd = DbConnUtil.GetPassword(connStr);
                        if ((!string.IsNullOrEmpty(uid)) && (!string.IsNullOrEmpty(pwd)))
                        {
                            authType = AuthenticationType.SQLServerAuthentication;
                        }
                    }
                }
                catch
                {
                    authType = AuthenticationType.WindowsAuthentication;
                }
            }
            else
            {
                authType = AuthenticationType.SQLServerAuthentication;
            }
            return authType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static string GetUserID(string connStr)
        {
            string uid = "";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("User\\s+ID\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match match = regex.Match(connStr);
            if (match.Success)
            {
                uid = match.Groups[1].Value;
            }
            else
            {
                regex = new System.Text.RegularExpressions.Regex("uid\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                match = regex.Match(connStr);
                if (match.Success)
                {
                    uid = match.Groups[1].Value;
                }
            }
            return uid;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <returns></returns>
        public static string GetPassword(string connStr)
        {
            string pwd = "";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("Password\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match match = regex.Match(connStr);
            if (match.Success)
            {
                pwd = match.Groups[1].Value;
            }
            else
            {
                regex = new System.Text.RegularExpressions.Regex("pwd\\s*=\\s*([^;=]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                match = regex.Match(connStr);
                if (match.Success)
                {
                    pwd = match.Groups[1].Value;
                }
            }
            return pwd;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="host"></param>
        /// <param name="instance"></param>
        /// <param name="dbName"></param>
        /// <param name="authType"></param>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static string BuildConnectionString(string host, string instance, string dbName, AuthenticationType authType, string uid, string pwd)
        {
            string connStr = "Data Source=" + host;
            if (!string.IsNullOrEmpty(instance))
            {
                connStr += "\\" + instance;
            }
            connStr += ";";
            connStr += "database=" + dbName + ";";
            if (authType == AuthenticationType.WindowsAuthentication)
            {
                connStr += "integrated security=true;";
            }
            else
            {
                if (string.IsNullOrEmpty(uid))
                {
                    throw new Exception("please provide uid for db connection");
                }
                if (pwd == null)
                {
                    pwd = string.Empty;
                }
                connStr += "uid=" + uid + ";pwd=" + pwd + ";";
            }
            return connStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        public static string EncryptPassword(string pass)
        {
            // TODO: implement DbConnUtil
            throw new NotImplementedException("Not implemented");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        public static string DecryptPassword(string pass)
        {
            // TODO: implement DbConnUtil
            throw new NotImplementedException("Not implemented");
        }
    }
}
